package com.lifeng.service.impl;

import com.alibaba.fastjson.JSON;
import com.lifeng.constants.TokenConstants;
import com.lifeng.entity.LoginUser;
import com.lifeng.service.TokenService;
import com.lifeng.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


/**
 * @author: lifeng
 * @description:
 **/
@Service
public class TokenServiceImpl implements TokenService {

	@Autowired
	private StringRedisTemplate redisTemplate;
	@Override
	public Map<String, Object> createToken(LoginUser loginUser) {
		String token = loginUser.getToken();
		//存到redis中
		redisTemplate.opsForValue().set(TokenConstants.REDIS_PREFIX + token, JSON.toJSONString(loginUser));

		//jwt加密后的用户信息返回到前端
		Map<String, Object> map = new HashMap<>();
		map.put(TokenConstants.JWT_TOKEN,token);
		String jwtToken = JwtUtil.createToken(map);
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put(TokenConstants.JWT_ACCESS_TOKEN,jwtToken);

		return resultMap;
	}

}
